package me.firstapp.api.shop;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.ProductSkuRelation;
import me.firstapp.service.shop.ProductSkuRelationService;

@Controller
@RequestMapping(value = "/productSkuRelation")
@Api(value = "/productSkuRelation", description = "商品属性关系相关接口")
public class ProductSkuRelationApi extends BaseApi {

	@Autowired
	private ProductSkuRelationService productSkuRelationService;

	@RequestMapping(value = "findProductSkuRelationPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询商品属性关系", notes = "分页查询商品属性关系")
	public void findProductSkuRelationPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<ProductSkuRelation> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<ProductSkuRelation> productSkuRelationPage = productSkuRelationService
					.findProductSkuRelationPage(pageNo, pageSize, searchParams, sortParam, sortType, apiClientId);
			JsonPage<ProductSkuRelation> jsonPage = new JsonPage<ProductSkuRelation>();
			jsonPage.setList(productSkuRelationPage.getEntities());
			jsonPage.setPageNo(productSkuRelationPage.getPageNo());
			jsonPage.setPageSize(productSkuRelationPage.getPageSize());
			jsonPage.setTotalCount(productSkuRelationPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/addProductSkuRelation.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增商品属性关系", notes = "新增商品属性关系")
	public void addProductSkuRelation(Long productId, String skuName1, String skuValue1, String skuName2,
			String skuValue2, Integer stock, Double price, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<ProductSkuRelation> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			ProductSkuRelation productSkuRelation = productSkuRelationService.addProductSkuRelation(productId, skuName1,
					skuValue1, skuName2, skuValue2, stock, price, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(productSkuRelation);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateProductSkuRelation.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新商品属性关系", notes = "更新商品属性关系")
	public void updateProductSkuRelation(Long productSkuRelationId, String skuName1, String skuValue1, String skuName2,
			String skuValue2, Integer stock, Double price, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<ProductSkuRelation> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			ProductSkuRelation productSkuRelation = productSkuRelationService.updateProductSkuRelation(
					productSkuRelationId, skuName1, skuValue1, skuName2, skuValue2, stock, price, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(productSkuRelation);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteProductSkuRelations.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除商品属性关系", notes = "删除商品属性关系")
	public void deleteProductSkuRelations(String productSkuRelationsIds, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			productSkuRelationService.deleteProductSkuRelations(productSkuRelationsIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findProductSkuRelationById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过id查询商品属性关系", notes = "通过id查询商品属性关系")
	public void findProductSkuRelationById(Long productSkuRelationId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<ProductSkuRelation> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			ProductSkuRelation productSkuRelation = productSkuRelationService
					.findProductSkuRelationById(productSkuRelationId, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(productSkuRelation);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/setDefaultProductSkuRelation.do", method = RequestMethod.POST)
	@ApiOperation(value = "设置默认商品属性关系", notes = "设置默认商品属性关系")
	public void setDefaultProductSkuRelation(Long productSkuRelationId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<ProductSkuRelation> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			ProductSkuRelation productSkuRelation = productSkuRelationService.executeSetDefault(productSkuRelationId,
					apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(productSkuRelation);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findSkuRelByProductId.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询商品属性关系", notes = "查询商品属性关系")
	public void findProductSkuRelationsByProductId(Long productId, Integer isDefault, HttpServletRequest request,
			HttpServletResponse response) {
		ListObject<ProductSkuRelation> result = new ListObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<ProductSkuRelation> relList = productSkuRelationService.findProductSkuRelationsByProductId(productId,
					isDefault, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(relList);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
